<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 3. 用户输入一个整数n：
    
    // 求1! + 2! + 3! + 4! + …… + n!的和
    // 叹号表示阶乘，如4! = 1 * 2 * 3 * 4

    // 求一个阶乘
    function factorial (num) {
      if (typeof(num) !== 'number') throw new Error('请传入数字')
      var result = 1
      for (var i = num; i >= 1; i--) {
        result *= i
      }
      return result
    }
    
    // 阶乘求和
    function getSumOfFactorial (num) {
      if (typeof(num) !== 'number') throw new Error('请传入数字')
      var sum = 0
      for (var i = 1; i <= num; i++) {
        sum += factorial(i)
      }
      return sum
    }
    console.log('求出6的阶乘和')
    console.log(getSumOfFactorial(6))
  </script>
</body>
</html>